﻿using System;

namespace MachiningHelper.ToolFile
{
    class SkipperEngine
    {
        public MachiningPart part1 { get; set; }
        public MachiningPart part2 { get; set; }
        protected double Length { get; set; }
        protected double Thickness { get; set; }

        public SkipperEngine(string file1, string file2)
        {
            part1 = new MachiningPart(file1);
            part2 = new MachiningPart(file2);

            Length = double.Parse(part2.Border.PanelLength);
            Thickness = double.Parse(part2.Border.PanelThickness);
        }

        public void DoublePartFlipOne()
        {
            AllowFlipPart();

            BorderSet();
            VdrillSet();
            HdrillSet();
            SawSet();
            RouteSet();
        }

        private void AllowFlipPart()
        {
            if (part1.Border.DoubleFace5 ||
                part1.Border.DoubleFace6 ||
                part2.Border.DoubleFace6 ||
                part2.Border.DoubleFace5 ||
                part1.Border.PanelLength != part2.Border.PanelLength ||
                part1.Border.PanelThickness != part2.Border.PanelThickness ||
                part1.Border.PanelWidth != part2.Border.PanelWidth)
                throw new Exception("FlipPart Forbidden!");
        }

        private void BorderSet()
        {
            part2.Border.PanelThickness = (2 * Thickness).ToString();
            part2.Border.Face6FileName = "unknow";
            part2.Border.FoundFace6Program = true;
            part2.Border.FileName = string.Empty;
            part2.Border.FoundVdrill = true;
        }

        private void VdrillSet()
        {
            if (part2.Border.Face6Only)
                return;

            var vdrills = part2.Vdirlls;
            foreach (var v in vdrills)
            {
                v.VDrillX = (Length - double.Parse(v.VDrillX)).ToString();
            }
        }

        private void HdrillSet()
        {
            if (part2.Border.Face5Only)
                return;

            var hdrills = part2.Hdrills;

            foreach (var h in hdrills)
            {
                h.HDrillZ = (Thickness - double.Parse(h.HDrillZ)).ToString();
                switch (h.CurrentFace)
                {
                    case "3":
                        h.CurrentFace = "4";
                        break;
                    case "4":
                        h.CurrentFace = "3";
                        break;
                    default:
                        h.HDrillX = (Length - double.Parse(h.HDrillX)).ToString();
                        break;
                }
            }
        }

        private void RouteSet()
        {
            if (part2.Border.Face6Only)
                return;
            var routes = part2.Routes;

            foreach (var r in routes)
            {
                r.SetMillSeq.RouteCenterX = string.IsNullOrEmpty(r.SetMillSeq.RouteCenterX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteCenterX)).ToString();
                r.SetMillSeq.RouteEndOffsetX = string.IsNullOrEmpty(r.SetMillSeq.RouteEndOffsetX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteEndOffsetX)).ToString();
                r.SetMillSeq.RouteNextX = string.IsNullOrEmpty(r.SetMillSeq.RouteNextX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteNextX)).ToString();
                r.SetMillSeq.RoutePreviousX = string.IsNullOrEmpty(r.SetMillSeq.RoutePreviousX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RoutePreviousX)).ToString();
                r.SetMillSeq.RouteSetMillX = string.IsNullOrEmpty(r.SetMillSeq.RouteSetMillX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteSetMillX)).ToString();
                r.SetMillSeq.RouteStartOffsetX = string.IsNullOrEmpty(r.SetMillSeq.RouteStartOffsetX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteStartOffsetX)).ToString();
                r.SetMillSeq.RouteX = string.IsNullOrEmpty(r.SetMillSeq.RouteX) ? string.Empty : (Length - double.Parse(r.SetMillSeq.RouteX)).ToString();
                r.SetMillSeq.RouteBulge = string.IsNullOrEmpty(r.SetMillSeq.RouteBulge) ? string.Empty : (-double.Parse(r.SetMillSeq.RouteBulge)).ToString();
                r.SetMillSeq.RouteBulgeNext = string.IsNullOrEmpty(r.SetMillSeq.RouteBulgeNext) ? string.Empty : (-double.Parse(r.SetMillSeq.RouteBulgeNext)).ToString();
                foreach (var sr in r.RouteSeqs)
                {
                    sr.RouteCenterX = string.IsNullOrEmpty(sr.RouteCenterX) ? string.Empty : (Length - double.Parse(sr.RouteCenterX)).ToString();
                    sr.RouteEndOffsetX = string.IsNullOrEmpty(sr.RouteEndOffsetX) ? string.Empty : (Length - double.Parse(sr.RouteEndOffsetX)).ToString();
                    sr.RouteNextX = string.IsNullOrEmpty(sr.RouteNextX) ? string.Empty : (Length - double.Parse(sr.RouteNextX)).ToString();
                    sr.RoutePreviousX = string.IsNullOrEmpty(sr.RoutePreviousX) ? string.Empty : (Length - double.Parse(sr.RoutePreviousX)).ToString();
                    sr.RouteSetMillX = string.IsNullOrEmpty(sr.RouteSetMillX) ? string.Empty : (Length - double.Parse(sr.RouteSetMillX)).ToString();
                    sr.RouteStartOffsetX = string.IsNullOrEmpty(sr.RouteStartOffsetX) ? string.Empty : (Length - double.Parse(sr.RouteStartOffsetX)).ToString();
                    sr.RouteX = string.IsNullOrEmpty(sr.RouteX) ? string.Empty : (Length - double.Parse(sr.RouteX)).ToString();
                    sr.RouteBulge = string.IsNullOrEmpty(sr.RouteBulge) ? string.Empty : (-double.Parse(sr.RouteBulge)).ToString();
                    sr.RouteBulgeNext = string.IsNullOrEmpty(sr.RouteBulgeNext) ? string.Empty : (-double.Parse(sr.RouteBulgeNext)).ToString();
                }
            }
        }

        private void SawSet()
        {
            if (part2.Border.Face6Only)
                return;

            var saws = part2.Saws;
            foreach (var s in saws)
            {
                s.SawEndOffsetX = string.IsNullOrEmpty(s.SawEndOffsetX) ? string.Empty : (Length - double.Parse(s.SawEndOffsetX)).ToString();
                s.SawEndX = string.IsNullOrEmpty(s.SawEndX) ? string.Empty : (Length - double.Parse(s.SawEndX)).ToString();
                s.SawStartOffsetX = string.IsNullOrEmpty(s.SawStartOffsetX) ? string.Empty : (Length - double.Parse(s.SawStartOffsetX)).ToString();
                s.SawStartX = string.IsNullOrEmpty(s.SawStartX) ? string.Empty : (Length - double.Parse(s.SawStartX)).ToString();
            }
        }

    }
}
